//
//  ChatListCell.m
//  PigMarket
//
//  Created by mac on 2019/6/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ChatListCell.h"

@interface ChatListCell ()
/**
 头像
 */
@property (nonatomic , strong) UIImageView *headerImg;
/**
 昵称
 */
@property (nonatomic , strong) UILabel *nameLab;
/**
 最近一条聊天记录
 */
@property (nonatomic , strong) UILabel *detailLab;

/**
 未读消息数
 */
@property (nonatomic , strong) UILabel *unreadNumLab;
/**
 灰线
 */
@property (nonatomic , strong) UIView *line;
@end
@implementation ChatListCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifierID = @"identifierID";
    ChatListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierID];
    if (cell == nil) {
        cell = [[ChatListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifierID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
#pragma mark - init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
        [self initLayout];
    }
    return self;
}

- (void)initView {
    // 添加子控件
    [self.contentView addSubview:self.headerImg];
//    [self.headerImg addSubview:self.unreadNumLab];
    [self.contentView addSubview:self.addStatusBtn];
    [self.contentView addSubview:self.nameLab];
    [self.contentView addSubview:self.detailLab];
    [self.contentView addSubview:self.line];
}

- (void)initLayout {
    // 布局子控件
    [self.headerImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
//    [self.unreadNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self.headerImg.mas_right);
//        make.centerY.mas_equalTo(self.headerImg.mas_top).offset(4);
//        make.size.mas_equalTo(CGSizeMake(20, 20));
//    }];
    [self.addStatusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(56 * [JTool getScreenWidthScale], 26));
    }];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headerImg.mas_right).offset(20);
        make.top.mas_equalTo(10);
        make.height.mas_equalTo(22);
        make.right.mas_equalTo(-10);
    }];
    [self.detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab.mas_left);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(6);
        make.height.mas_equalTo(19);
        make.right.mas_equalTo(-15);
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab.mas_left);
        make.bottom.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.right.mas_equalTo(0);
    }];
}
- (void)addFriendBtnClick {
    if (self.agreeAddBlock) {
        self.agreeAddBlock();
    }
}
//添加豚友页面
- (void)configAddFriendCellWithIndex:(NSInteger)index {
    if (index == 0) {
        self.headerImg.image = [UIImage imageNamed:@"chat_addfriend_addressbook"];
        self.nameLab.text = @"手机联系人";
        self.detailLab.text = @"添加手机通讯录中的豚友";

    } else if (index == 1) {
        self.headerImg.image = [UIImage imageNamed:@"chat_nearbyfriend"];
        self.nameLab.text = @"附近豚友";
        self.detailLab.text = @"添加附近的豚友";
    }
    self.addStatusBtn.hidden = YES;
}
#pragma mark - getter and setter
//手机通讯录朋友
- (void)setAddressBookModel:(AddressBookModel *)addressBookModel {
    _addressBookModel = addressBookModel;
    
}
//新的豚友
- (void)setAddNewFriendModel:(TIMFriendPendencyItem *)addNewFriendModel{
    _addNewFriendModel = addNewFriendModel;
    [self.headerImg sd_setImageWithURL:[NSURL URLWithString:addNewFriendModel.addWording] placeholderImage:[UIImage imageNamed:@"placeholder_mheader"]];
    self.nameLab.text = addNewFriendModel.nickname;
    //显示时间
    self.detailLab.text = [NSString transferTimeStamp:addNewFriendModel.addTime];
    [self.addStatusBtn setTitle:@"添加好友" forState:UIControlStateNormal];

}
//好友通讯录
- (void)setFriendModel:(Friends *)friendModel {
    _friendModel = friendModel;
//    if ([friendModel.headImage. isEqualToString:@"chat_addfriends"] || [friendModel.headImg isEqualToString:@"chat_mygroup"]) {
//        [self.nameLab mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(23);
//        }];
//        self.headerImg.image = friendModel.headImage;
//        self.detailLab.hidden = YES;
//    } else {
        [self.nameLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
        }];
        self.headerImg.image = friendModel.headImage;
        self.detailLab.text = friendModel.personality;
//    }
    NSString *modifyName = friendModel.noteName;
    NSString *nickName = friendModel.nickName;
    NSString *name = modifyName ? modifyName : nickName;
    self.nameLab.text = name;
    self.unreadNumLab.hidden = YES;
    self.addStatusBtn.hidden = YES;
}
//群聊列表
- (void)setGroupModel:(TIMGroupInfo *)groupModel {
    _groupModel = groupModel;
    [self.headerImg sd_setImageWithURL:[NSURL URLWithString:groupModel.faceURL] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    self.nameLab.text = groupModel.groupName;
    
    self.detailLab.text = groupModel.notification;
    self.addStatusBtn.hidden = YES;
}
//搜索添加好友
- (void)setSearchAddFriendModel:(SearchAddFriend *)searchAddFriendModel {
    _searchAddFriendModel = searchAddFriendModel;
    [self.headerImg sd_setImageWithURL:[NSURL URLWithString:searchAddFriendModel.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_header"]];
    self.nameLab.text = searchAddFriendModel.nickName;
    //显示时间
    self.detailLab.text = searchAddFriendModel.personality;
    [self.addStatusBtn setTitle:@"查看" forState:UIControlStateNormal];
    [self.addStatusBtn setTintColor:[UIColor colorWithHexString:@"#B4B4B4"]];
    self.addStatusBtn.layer.borderWidth = 0;
}
- (UIImageView *)headerImg {
    if (!_headerImg) {
        _headerImg = [[UIImageView alloc] init];
        _headerImg.layer.cornerRadius = 25;
        _headerImg.layer.masksToBounds = YES;
        _headerImg.backgroundColor = [UIColor randomColor];
    }
    return _headerImg;
}
- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.textColor = [UIColor textBlackColor];
        _nameLab.font = [UIFont largerFont];
    }
    return _nameLab;
}
//- (UILabel *)unreadNumLab {
//    if (!_unreadNumLab) {
//        _unreadNumLab = [[UILabel alloc] init];
//        _unreadNumLab.textColor = [UIColor colorWithHexString:@"#EE5B5B"];
//        _unreadNumLab.font = [UIFont smallFont];
//        _unreadNumLab.layer.borderWidth = 1;
//        _unreadNumLab.layer.borderColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0].CGColor;
//        _unreadNumLab.layer.backgroundColor = [UIColor colorWithRed:238/255.0 green:91/255.0 blue:91/255.0 alpha:1.0].CGColor;
//        _unreadNumLab.layer.cornerRadius = 40;
//    }
//    return _unreadNumLab;
//}
- (UILabel *)detailLab {
    if (!_detailLab) {
        _detailLab = [[UILabel alloc] init];
        _detailLab.textColor = [UIColor colorWithHexString:@"#999999"];
        _detailLab.font = [UIFont middleFont];
    }
    return _detailLab;
}
- (UIButton *)addStatusBtn {
    if (!_addStatusBtn) {
        _addStatusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addStatusBtn.titleLabel.font = [UIFont systemFontOfSize:11];
        [_addStatusBtn setTitleColor:[UIColor primaryColor] forState:UIControlStateNormal];
        _addStatusBtn.layer.cornerRadius = 5;
        _addStatusBtn.layer.borderColor = [UIColor primaryColor].CGColor;
        _addStatusBtn.layer.borderWidth = 1;
        [_addStatusBtn addTarget:self action:@selector(addFriendBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addStatusBtn;
}
- (UIView *)line {
    if ((!_line)) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _line;
}
@end
